package cn.getech.data.metadata.service.impl;

import cn.getech.data.intelligence.common.exception.RRException;
import cn.getech.data.intelligence.common.utils.PageUtils;
import cn.getech.data.intelligence.common.utils.Query;
import cn.getech.data.metadata.entity.CollectionTaskAlarmReceiver;
import cn.getech.data.metadata.mapper.CollectionTaskAlarmReceiverMapper;
import cn.getech.data.metadata.model.req.change.CollectionTaskAlarmReq;
import cn.getech.data.metadata.model.res.CollectionSystemModuleRes;
import cn.getech.system.center.utils.ShiroUtils;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.getech.data.metadata.mapper.CollectionTaskAlarmMapper;
import cn.getech.data.metadata.entity.CollectionTaskAlarm;
import cn.getech.data.metadata.service.CollectionTaskAlarmService;
import org.apache.kafka.common.protocol.types.Field;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * (CollectionTaskAlarm)表服务实现类
 *
 * @author makejava
 * @since 2021-08-12 18:53:43
 */
@Service("collectionTaskAlarmService")
public class CollectionTaskAlarmServiceImpl extends ServiceImpl<CollectionTaskAlarmMapper, CollectionTaskAlarm> implements CollectionTaskAlarmService {

    @Autowired
    CollectionTaskAlarmReceiverMapper collectionTaskAlarmReceiverMapper;

    @Override
    public IPage<CollectionTaskAlarm> listBy(CollectionTaskAlarmReq req) {
        Page<CollectionTaskAlarm> page = new Page<>(req.getCurrent(), req.getSize());
        IPage<CollectionTaskAlarm> iPage = this.baseMapper.listBy(page, req);
        return iPage;
    }

    @Override
    public CollectionTaskAlarm getInfo(Long id) {
        CollectionTaskAlarm alarm = this.baseMapper.getInfo(id);
        List<CollectionTaskAlarmReceiver> list = collectionTaskAlarmReceiverMapper.
                selectList(new QueryWrapper<CollectionTaskAlarmReceiver>().eq("alarm_id", id));
        if (alarm.getNoDisturbingStart() != null) {
            alarm.setNoDisturbingStartTime(DateUtil.format(alarm.getNoDisturbingStart(), "HH:mm:ss"));
            alarm.setNoDisturbingStart(null);
        }
        if (alarm.getNoDisturbingEnd() != null) {
            alarm.setNoDisturbingEndTime(DateUtil.format(alarm.getNoDisturbingEnd(), "HH:mm:ss"));
            alarm.setNoDisturbingEnd(null);
        }
        alarm.setAlarmObjectList(list);
        return alarm;
    }

    @Override
    @Transactional
    public void add(CollectionTaskAlarm req) {
        CollectionTaskAlarm alarm = this.baseMapper.selectOne(new QueryWrapper<CollectionTaskAlarm>().eq("monitor_name", req.getMonitorName()));
        if (alarm != null) {
            throw new RRException("任务告警名字已经存在");
        }
        if (StrUtil.isNotBlank(req.getNoDisturbingStartTime())) {
            req.setNoDisturbingStart(DateUtil.parse("1970-01-01 " + req.getNoDisturbingStartTime(), "yyyy-MM-dd HH:mm:ss"));
        }
        if (StrUtil.isNotBlank(req.getNoDisturbingEndTime())) {
            req.setNoDisturbingEnd(DateUtil.parse("1970-01-01 " + req.getNoDisturbingEndTime(), "yyyy-MM-dd HH:mm:ss"));
        }
        req.setUpdateTime(new Date());
        req.setCreateId(Long.valueOf(ShiroUtils.getUserId()));
        this.baseMapper.insert(req);
        if (req.getAlarmObjectList() != null) {
            for (CollectionTaskAlarmReceiver receiver : req.getAlarmObjectList()) {
                receiver.setAlarmId(req.getId());
                collectionTaskAlarmReceiverMapper.insert(receiver);
            }
        }
    }

    @Override
    @Transactional
    public void upd(CollectionTaskAlarm req) {
        if (req.getId() == null) {
            throw new RRException("id不能为空");
        }
        CollectionTaskAlarm alarm = this.baseMapper.selectOne(new QueryWrapper<CollectionTaskAlarm>()
                .eq("monitor_name", req.getMonitorName())
                .ne("id", req.getId())
        );
        if (alarm != null) {
            throw new RRException("任务告警名字已经存在");
        }
        if (StrUtil.isNotBlank(req.getNoDisturbingStartTime())) {
            req.setNoDisturbingStart(DateUtil.parse("1970-01-01 " + req.getNoDisturbingStartTime(), "yyyy-MM-dd HH:mm:ss"));
        }
        if (StrUtil.isNotBlank(req.getNoDisturbingEndTime())) {
            req.setNoDisturbingEnd(DateUtil.parse("1970-01-01 " + req.getNoDisturbingEndTime(), "yyyy-MM-dd HH:mm:ss"));
        }
        collectionTaskAlarmReceiverMapper.delete(new QueryWrapper<CollectionTaskAlarmReceiver>().eq("alarm_id", req.getId()));
        req.setUpdateId(Long.valueOf(ShiroUtils.getUserId()));
        req.setUpdateTime(new Date());
        this.baseMapper.updateById(req);
        if (req.getAlarmObjectList() != null) {
            for (CollectionTaskAlarmReceiver receiver : req.getAlarmObjectList()) {
                receiver.setAlarmId(req.getId());
                collectionTaskAlarmReceiverMapper.insert(receiver);
            }
        }
    }

    @Override
    @Transactional
    public void del(Long id) {
        collectionTaskAlarmReceiverMapper.delete(new QueryWrapper<CollectionTaskAlarmReceiver>().eq("alarm_id", id));
        this.baseMapper.deleteById(id);
    }
}
